package com.crm.controller.permission;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crm.exception.ServiceException;
import com.crm.pojo.CrmDept;
import com.crm.pojo.Userinfo;
import com.crm.service.permission.UserInfoService;
import com.crm.vo.Result;

@Controller
public class UserInfoController {

	@Autowired
	private UserInfoService userInfoService;

	/**
	 * 分页查询所有部门
	 * 
	 * @param page
	 * @param rows
	 * @return
	 */
	@PostMapping("query-All-PageCount")
	@ResponseBody
	public Map queryAllPageCount(int page, int rows) {
		Long total = userInfoService.queryPageCount();
		List<Userinfo> list = userInfoService.queryAllUser(page, rows);
		Map map = new HashMap<>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}

	/**
	 * 根据用户名模糊查询
	 * 
	 * @param uname
	 * @return
	 */
	@PostMapping("query-Some-Name")
	@ResponseBody
	public Map querySomeName(String uname, int page, int rows) {
		Long total = userInfoService.queryNamePageCount(uname);
		List<Userinfo> list = userInfoService.queryNameUsernifo(uname, page, rows);
		Map map = new HashMap<>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}

	/**
	 * 添加用户
	 * 
	 * @param uname
	 * @param upassword
	 * @param urealName
	 * @param phone
	 * @param ucreateTime
	 * @param crmDeptid
	 */
	@PostMapping("add-User-Infos")
	@ResponseBody
	public Result addUserInfos(String uname, String upassword, String urealName, String phone, String userAddress,
			CrmDept crmDeptno) {
		try {
			Userinfo userinfo = userInfoService.queryOneName(uname);
			if (null == userinfo) {
				userInfoService.addUserInfo(uname, upassword, urealName, phone, userAddress, crmDeptno);
				return Result.SUCCESS;
			} else {
				return new Result("添加失败，用户名重复！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new Result("添加失败，用户名重复！");
		}

	}

	/**
	 * 删除一个用户
	 * 
	 * @param uId
	 */
	@PostMapping("delete-One-User")
	@ResponseBody
	public Result deleteOneUser(Long uId) {
		try {
			String name = userInfoService.queryRoleNameUsers(uId);
			if(name.equals("超级管理员")){
				return new Result("该用户是超级管理员，不能被删除");
			}else{
				userInfoService.deleteUserInfo(uId);
				return Result.SUCCESS;
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(e);
		}
	}

	/**
	 * 重置用户密码为123456
	 * 
	 * @param uId
	 * @return
	 */
	@PostMapping("reset-OnePassword")
	@ResponseBody
	public Result resetOnePassword(Long uId) {
		try {
			userInfoService.resetPassword(uId);
			return Result.SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(e);
		}
	}

	/**
	 * 根据用户ID查找用户信息
	 * 
	 * @param uId
	 * @return
	 */
	@GetMapping("get-One-User")
	@ResponseBody
	public Userinfo getOneUser(Long uId) {
		Userinfo ui = userInfoService.getOne(uId);
		ui.setCrmDeptno(ui.getCrmDept().getCrmDeptno());
		return ui;
	}

	/**
	 * 修改用户信息
	 * 
	 * @param uId
	 * @param uname
	 * @param upassword
	 * @param urealName
	 * @param phone
	 * @param userAddress
	 * @param CrmDeptno
	 * @param ustatus
	 */
	@PostMapping("update-User-Info")
	@ResponseBody
	public Result updateUserInfo(Long uId, String uname, String urealName, String phone, String userAddress,
			CrmDept CrmDeptno) {
		try {
			Userinfo userinfo = userInfoService.findOne(uId);
			
			List<String> list = userInfoService.queryNameAllUpdate();
			int num=0;
			for (int i = 0; i < list.size(); i++) {
				if(list.get(i).equals(uname)){
					num++;
				}
			}
			if(userinfo.getUname().equals(uname)){
				userInfoService.updateUser(uId, uname, urealName, phone, userAddress, CrmDeptno);
				return Result.SUCCESS;
			} else if (num == 0) {
				userInfoService.updateUser(uId, uname, urealName, phone, userAddress, CrmDeptno);
				return Result.SUCCESS;
			} else{
				return new Result("修改失败，用户名重复！");
			}
			
		} catch (ServiceException e) {
			e.printStackTrace();
			return new Result("修改失败，用户名重复！");
		}
	}

	@GetMapping("userinfo")
	public String toUserInfo() {
		return "permission/user-info";
	}

	@GetMapping("user-add")
	public String toUserAdd() {
		return "permission/user-add";
	}

	@GetMapping("user-update")
	public String toUserUpdate() {
		return "permission/user-update";
	}

}
